//
//  Date+Extend.swift
//  TSProfitProject
//
//  Created by TMS on 2019/11/14.
//  Copyright © 2019 TMS. All rights reserved.
//

import UIKit

extension Date {
    
    static func currentTime(formatter:String = "YYYY-MM-dd") -> String {

        let dateformatter = DateFormatter()
        dateformatter.dateFormat = formatter// 自定义时间格式
        // GMT时间 转字符串，直接是系统当前时间
        return dateformatter.string(from: Date.getNowDateFromatAnDate(Date()))
    }
    
    // 获取当前时间的年和月
    static func getCurrentYearAndMonth() -> (Int,Int) {
        let calendar = Calendar.current
        let dateComponets = calendar.dateComponents([Calendar.Component.year,Calendar.Component.month,Calendar.Component.day], from: Date())
        return (dateComponets.year!, dateComponets.month!)
    }
    
    // String转Date
    static func stringConvertDate(string:String, dateFormat:String="yyyy-MM-dd") -> Date {
        
        let formatter = DateFormatter()
        formatter.locale = Locale.init(identifier: "zh_CN")
        formatter.dateFormat = dateFormat
        let date = formatter.date(from: string)
        return Date.getNowDateFromatAnDate(date)
    }
    
    // 时区转换
    static func getNowDateFromatAnDate(_ anyDate: Date?) -> Date {
        //设置源日期时区
        let sourceTimeZone = NSTimeZone(abbreviation: "UTC")
        //或GMT
        //设置转换后的目标日期时区
        let destinationTimeZone = NSTimeZone.local as NSTimeZone
        //得到源日期与世界标准时间的偏移量
        var sourceGMTOffset: Int? = nil
        if let aDate = anyDate {
            sourceGMTOffset = sourceTimeZone?.secondsFromGMT(for: aDate)
        }
        //目标日期与本地时区的偏移量
        var destinationGMTOffset: Int? = nil
        if let aDate = anyDate {
            destinationGMTOffset = destinationTimeZone.secondsFromGMT(for: aDate)
        }
        //得到时间偏移量的差值
        let interval = TimeInterval((destinationGMTOffset ?? 0) - (sourceGMTOffset ?? 0))
        //转为现在时间
        var destinationDateNow: Date? = nil
        if let aDate = anyDate {
            destinationDateNow = Date(timeInterval: interval, since: aDate)
        }
        return destinationDateNow!
    }
    
    // String转时间戳
    static func dateStrToTimeInterval(dateStr: String) -> Int  {
        let dateformatter = DateFormatter()
        dateformatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        let date = Date.getNowDateFromatAnDate(dateformatter.date(from: dateStr))
        let dateTimeInterval:TimeInterval = date.timeIntervalSince1970
        return Int(dateTimeInterval)
    }
    
    // 时间戳转时间函数
    static func timeStampToString(timeStamp: Int64, outputFormatter: String = "yyyy-MM-dd HH:mm:ss") -> String {
        let timeString = String.init(format: "%d", timeStamp)

        let timeSta:TimeInterval

        if timeString.count == 10 {
            timeSta = TimeInterval(timeStamp)
        }else{
            timeSta = TimeInterval(timeStamp / 1000)
        }

        let date = NSDate(timeIntervalSince1970: timeSta)
        let dfmatter = DateFormatter()
        //设定时间格式,这里可以设置成自己需要的格式yyyy-MM-dd HH:mm:ss
        dfmatter.dateFormat = outputFormatter
        return dfmatter.string(from: date as Date)
    }
    
    // 比较两个时间的大小
    static func compareTimeOfSize(startDate:Date, endDate:Date) -> Bool{
        // date1 < date2 升序排列
        if startDate.compare(endDate) == .orderedAscending
        {
              print("<")
            return true
        }
        //date1 = date2 相等
        if startDate.compare(endDate) == .orderedSame
        {
              print(" = ")
              return true
        }
        //降序排列的 date1 > date2 降序排列
        if startDate.compare(endDate) == .orderedDescending
        {
            print("<")
            CBToast.showToast(message: "起始查询时间必须小于等于终止查询时间", aLocationStr: "center", aShowTime: 2.5)
            return false
        }
        return false
    }
    
    //MARK:获取当前时间与一个时间的差值
    static func getTimeDifferentWith(date: Date) -> Int {
        
        let calendar = NSCalendar.current as NSCalendar
        let delta = calendar.components(.second, from: date, to: Date.getNowDateFromatAnDate(Date()), options: [])
        return delta.second ?? 0
    }
    
    static func compareCurrntTime(timeStamp:String) -> String {
        //计算出时间戳距离现在时间的一个秒数(..s)
        let interval:TimeInterval=TimeInterval(timeStamp)!
        let date = Date (timeIntervalSince1970: interval)
        var timeInterval = date.timeIntervalSinceNow
        //得到的是一个负值 (加' - ' 得正以便后面计算)
        timeInterval = -timeInterval
        //根据时间差 做所对应的文字描述 (作为返回文字描述)
        var result:String
            //一分钟以内
        if interval < 60{
            result = "刚刚"
            return result
        } else if Int(timeInterval/60) < 60{
            //一小时以内
            result = String.init(format:"%@分钟前",String(Int(timeInterval/60)))
            return result
        } else if Int((timeInterval/60)/60) < 24{
            //一天以内
            result = String.init(format:"%@小时前",String(Int((timeInterval/60)/60)))
            return result
        }else{
            //超过一天的
            let dateformatter = DateFormatter()
            //自定义日期格式
            dateformatter.dateFormat="yyyy年MM月dd日 HH:mm"
            result = dateformatter.string(from: date as Date)
            return result
        }
    }
}
